Online-Academy
Look, Read, Understand, Apply

OOP - Classes

class person:
    name = 'Henry'
    age = 20

class Employee():
    # __init__ is like constructor in JAVA, C++
    # it is called when the class is initiated
    # __int__ is used to perform operations that are necessary when the object is created
    # self parameter references current object (instance) of a class
    # self is used to access properties and methods of a class
    # without self Python would not know which object's properties being accessed
    def __init__(self,name,age=12):  #age is provided default value
        self.name = name
        self.age = age

class office:
    def __init__(self,name,address):
        self.name = name
        self.address = address

    def show(self): #method of a class
        #print(self.name," ",self.address)
        return self.name," ",self.address
    def sayhello(self):
        print("hello: ",self.show())  # calling method show

#it is not necessary to name self as self, we can give other name also
#but it has become convention to give name self to recognize object of a class

class car:
    def __init__(car,model,year):
        car.model = model
        car.year = year
    def show(car):
        print(car.model," ",car.year)

class Test:
    def __init__(self,name):
        self.__name = name
    def get_name(self):
        return self.__name

if __name__ == '__main__':
    test = Test("Julia")
    test.__name = "jack"
    print(test.get_name())
    c = car("Volvo",2025)
    c.show()

    o = office("ABC","Baneshwor")
    o.sayhello()
    p = person()
    print(p.name," ",p.age)
    p.name = "Columbus"
    p.age = 30
    print(p.name," ",p.age)
    # del p    # deleting object
    print(p.name," ",p.age)

    e1 = Employee("Henry",20)
    print(e1.name," ",e1.age)